﻿using System;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using LTJ.Entity;
using LTJ.Common;
using LTJ.Config;
using LTJ.FrameWork;

namespace LTJ.WebPage.SystemManage.route
{
    /// <summary>
    /// 整柜规则设置
    /// </summary>
    public partial class fclruleset : AdminPage
    {
        protected string opmode = SASRequest.GetString("opmode");
        protected int rid = SASRequest.GetInt("rid", 0);
        protected RuleInfo rinfo = new RuleInfo();
        protected int ruletype = 1;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void editrule_Click(object sender, EventArgs e)
        {
            if (opmode == "add")
            {
                ruletype = SASRequest.GetInt("objtype", 1);
            }
            rinfo.uid = -1;
            if (ruletype == 3 )
            {
                rinfo.oid = 0;
            }
            else if (ruletype == 4)
            {
                rinfo.oid = -1;
            }
            else
            {
                rinfo.oid = SASRequest.GetInt("ajaxcorpdatalist_value", 0);
            }
            rinfo._20GP = price1.Text;
            rinfo._40GP = price2.Text;
            rinfo._40HQ = price3.Text;
            if (ruletype == 1)
            {
                rinfo.rid = SASRequest.GetInt("ajaxcorproutedatalist_value", 0);
                rinfo.port = SASRequest.GetString("ajaxcorpportdatalist_value");
                rinfo.rtype = SASRequest.GetInt("endtype", 0);
                rinfo.seacode = "";
            }
            else if (ruletype == 4)
            {
                rinfo.rid = 0;
                rinfo.rtype = 0;
                rinfo.port = "";
                rinfo.seacode = SASRequest.GetString("ajaxroutelinedatalist_value");
            }
            else
            {
                rinfo.rtype = 0;
                rinfo.rid = 0;
                rinfo.port = "";
                rinfo.seacode = "";
            }

            if (ruletype == 1 && rinfo.rid == 0)
            {
                base.RegisterStartupScript("PAGETEMPLATE", "请选择授权航线！");
                return;
            }
            else if (rinfo.rtype == 2 && rinfo.port == "")
            {
                base.RegisterStartupScript("PAGETEMPLATE", "港口类型的规则，请选择相应港口！");
                return;
            }
            else if (rinfo.rtype == 0 && rinfo.oid == -1 && rinfo.uid == -1 && rinfo.rid == 0 && rinfo.seacode == "")
            {
                base.RegisterStartupScript("PAGETEMPLATE", "请选择航线！");
                return;
            }

            if (opmode == "add")
            {
                rinfo.id = Rules.CreateRules(rinfo.uid, rinfo.oid, rinfo.rtype, rinfo._20GP, rinfo._40GP, rinfo._40HQ, rinfo.rid, rinfo.port, rinfo.seacode);
                if (rinfo.id == -1)
                {
                    base.RegisterStartupScript("PAGETEMPLATE", "该航线规则已经存在，请在规则列表中查找进行修改！");
                    return;
                }
            }
            else if (opmode == "edit")
            {
                Rules.UpdateRulePrice(rinfo.id, price1.Text, price2.Text, price3.Text);
            }

            base.RegisterStartupScript("PAGE", "parent.document.getElementById('reclick').click();parent.Lightview.hide();");
        }

        #region Web 窗体设计器生成的代码

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            if (opmode == "edit")
            {
                rinfo = Rules.GetRuleInfoByID(rid);
                price1.Text = rinfo._20GP;
                price2.Text = rinfo._40GP;
                price3.Text = rinfo._40HQ;
                if (rinfo.uid == -1 && rinfo.rid == 0 && rinfo.oid == -1 && rinfo.rtype == 0)
                {
                    ruletype = 4;
                }
                else if (rinfo.uid == -1 && rinfo.rid == 0 && rinfo.oid == 0 && rinfo.rtype == 0)
                {
                    ruletype = 3;
                }
                else if (rinfo.uid == -1 && rinfo.rid == 0 && rinfo.rtype == 0)
                {
                    ruletype = 2;
                }
                else if (rinfo.uid == -1)
                {
                    ruletype = 1;
                }
            }

            editrule.Click += new EventHandler(editrule_Click);
        }

        #endregion
    }
}